package com.neuedu.web.service;

import com.neuedu.web.common.HisConstants;
import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.CheckApplyDao;
import com.neuedu.web.dao.ItemUploadDao;
import com.neuedu.web.dao.RegisterDao;
import com.neuedu.web.dao.UploadDao;
import com.neuedu.web.po.*;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.DeptVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 金山
 * 项目：his-server
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/12/511:46
 */
public class YijiService {
    RegisterDao registerDao = new RegisterDao();
    CheckApplyDao checkApplyDao = new CheckApplyDao();

    //上传信息文件
    ItemUploadDao itemUploadDao = new ItemUploadDao();

    UploadDao uploadDao = new UploadDao();
    /**
     * 查询患者列表的分页
     * @param request
     * @param response
     * @return
     */
    public PageInfo<RegisterPO> registPage(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        StringBuffer sql =new StringBuffer( " select * from register  " );
        String name = request.getParameter("name");
        System.out.println("name = " + name);
        Object params= null;
        int total = 0;

        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<RegisterPO> dataList = null;
        if(name != null  && name.trim().length() > 0){
            sql.append("where  name like ? ");
            params = "%"+name+"%";

            dataList = registerDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql.toString(),params);
            total = registerDao.total(sql.toString(),params);
        }else{
             dataList = registerDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql.toString());
            total = registerDao.total(sql.toString());
        }




        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(BeanConvertUtils.po2Vo(dataList, RegisterPO.class));

        return pageInfo;





    }

    public Map queryRegistCheckDetail(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String registId = request.getParameter("registId");

        //患者表单
        RegisterPO registerPO = registerDao.selectById(Long.valueOf(registId));

        //已开立检查
        List<CheckApplyPO> checkApplyPOS = checkApplyDao.selectAll("select * from check_apply where regist_id = ?", registId);


        Map result = new HashMap();

        result.put("registerPO",registerPO);
        result.put("checkApplyPOS",checkApplyPOS);
        return result;
    }

    public boolean saveCheckResult(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        String itemId = request.getParameter("itemId");
        String result = request.getParameter("result");
        String strUploadIds = request.getParameter("uploadIds");

        String[] uploadIds = strUploadIds.split(",");

        CheckApplyPO applyPO = checkApplyDao.selectById(Long.valueOf(itemId));
        applyPO.setResult(result);

        //修改状态 已执行
        applyPO.setStatus(HisConstants.CHECK_APPLAY_STATUS_3);


        int count = checkApplyDao.update(applyPO) ;

        for (String uploadId : uploadIds) {
            ItemUploadPO itemUploadPo = new ItemUploadPO();
            itemUploadPo.setItemId(Long.valueOf(itemId));
            itemUploadPo.setUploadId(Long.valueOf(uploadId));
            itemUploadPo.setType(HisConstants.UPLOAD_ITEM_TYPE_CHECK);

            itemUploadDao.insert(itemUploadPo);
        }



        return count>0 ;

    }



    public Map queryCheckResult(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        Map result = new HashMap();
        String itemId = request.getParameter("itemId");

        CheckApplyPO applyPO = checkApplyDao.selectById(Long.valueOf(itemId));




        String sql = "SELECT  * FROM upload WHERE upload_id IN ( SELECT upload_id FROM item_upload WHERE item_id = ?  AND TYPE = ? )";
        List<UploadPO> uploadPoList = uploadDao.selectAll(sql, itemId, HisConstants.UPLOAD_ITEM_TYPE_CHECK);

        result.put("applyPO",applyPO);
        result.put("uploads",uploadPoList);


        return result ;

    }

}
